package com.campusbackend.controller;

import com.campusbackend.entity.CarouselImage;
import com.campusbackend.entity.Result;
import com.campusbackend.service.CarouselImageService;
import com.campusbackend.utils.ResultUtil;
import com.campusbackend.vo.PublicCarouselImageVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 公共轮播图控制器（学生端和教师端使用）
 *
 * @author yezi
 * @since 2025-07-29
 */
@Slf4j
@RestController
@RequestMapping("/public")
@Tag(name = "公共轮播图", description = "公共轮播图相关接口（无需认证）")
public class PublicCarouselController {

    @Autowired
    private CarouselImageService carouselImageService;

    /**
     * 获取启用的轮播图列表
     * @return 启用的轮播图列表
     */
    @GetMapping("/carousel")
    @Operation(summary = "获取启用的轮播图列表", description = "获取启用且有图片的轮播图，按位置排序")
    public Result<List<PublicCarouselImageVO>> getActiveCarouselImages() {
        log.info("获取启用轮播图列表请求");
        
        try {
            List<CarouselImage> carouselImages = carouselImageService.getActiveCarouselImages();
            
            // 转换为公共VO对象
            List<PublicCarouselImageVO> carouselImageVOs = carouselImages.stream()
                    .map(carouselImage -> {
                        PublicCarouselImageVO vo = new PublicCarouselImageVO();
                        BeanUtils.copyProperties(carouselImage, vo);
                        return vo;
                    })
                    .collect(Collectors.toList());
            
            log.info("返回启用轮播图数量: {}", carouselImageVOs.size());
            return ResultUtil.success(carouselImageVOs);
        } catch (Exception e) {
            log.error("获取启用轮播图列表异常", e);
            return ResultUtil.fail("获取轮播图列表时发生错误");
        }
    }
}
